<!doctype html>
<html>

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title>底部导航</title>
    <link rel="stylesheet" type="text/css" href="./css/api.css" />
    <link rel="stylesheet" type="text/css" href="./css/style.css" />
    <style>
        header {
            background-color: #f2f2f2;
        }

        header ul li {
            height: 50px;
            line-height: 50px;
            text-align: center;
            display: none;
            color: #323237;
            position: relative;
            font-size: 18px;
        }

        header ul li.active {
            display: block;
        }

        #footer {
            background-color: #f2f2f2;
        }

        #footer ul li {
            padding-top: 36px;
            padding-bottom: 4px;
            background: url() no-repeat center 2px;
            background-size: auto 30px;
            text-align: center;
        }

        #footer ul li.active {
            color: #6ab494;
        }

        #footer ul li:nth-child(1) {
            background-image: url(./image/bottombtn0101.png);
        }

        #footer ul li:nth-child(2) {
            background-image: url(./image/bottombtn0201.png);
        }

        #footer ul li:nth-child(3) {
            background-image: url(./image/bottombtn0301.png);
        }

        #footer ul li:nth-child(4) {
            background-image: url(./image/bottombtn0401.png);
        }

        #footer ul li:nth-child(1).active {
            background-image: url(./image/bottombtn0102.png);
        }

        #footer ul li:nth-child(2).active {
            background-image: url(./image/bottombtn0202.png);
        }

        #footer ul li:nth-child(3).active {
            background-image: url(./image/bottombtn0302.png);
        }

        #footer ul li:nth-child(4).active {
            background-image: url(./image/bottombtn0402.png);
        }

        .flex-con {
            overflow: auto
        }
    </style>
</head>
<div class="header-d"></div>

<body>
    <div id="wrap" class="flex-wrap flex-vertical">
        <header>
            <ul>
                <li class="border-b active">云API</li>
                <li class="border-b">端API</li>
                <li class="border-b">开发</li>
                <li class="border-b">文档</li>
            </ul>
        </header>
        <div id="main" class="flex-con">

        </div>
        <div id="footer" class="border-t">
            <ul class="flex-wrap">
                <!-- <li tapmode="hover" onclick="();" class="flex-con active">复制</li>
                <li tapmode="hover" onclick="opendb();" class="flex-con">执行sql</li>
                <li tapmode="hover" onclick="select();" class="flex-con">查询</li>
                <li tapmode="hover" onclick="dele();" class="flex-con">删除</li> -->
            </ul>
        </div>
    </div>
</body>

</html>
<script type="text/javascript" src="script/api.js"></script>
<script type="text/javascript">
    //行政/事业(参公/行政支持/公益/经营)
    // SELECT COUNT(I44) FROM base_info WHERE I30
    function dele() {
        var db = api.require('db');
        db.executeSql({
            name: 'test',
            sql: ''
        }, function(ret, err) {
            if (ret.status) {
                alert(JSON.stringify(ret));
            } else {
                alert(JSON.stringify(err));
            }
        });
    }


    function remove() {
      var fs = api.require('fs');
      fs.remove({
          path: 'file:///storage/emulated/0/ader/sql'
      }, function(ret, err) {
          if (ret.status) {
              // alert(JSON.stringify(ret));
          } else {
              // alert(JSON.stringify(err));
          }
      });
    }


    function copy() {
        var fs = api.require('fs');
        fs.copyTo({
            oldPath: 'file:///storage/emulated/0/ader/sql',
            newPath: 'fs://'
        }, function(ret, err) {
            if (ret.status) {
                openDatabase();
                remove()
            } else {
                // alert(JSON.stringify(err));
                // openDatabase();
            }
        });
    }
    var res;

    function opendb() {
        var fs = api.require('fs');
        fs.open({
            path: 'fs://res/temp.sql',
            flags: 'read_write'
        }, function(ret, err) {
            if (ret.status) {
                res = ret.fd;
                //-----------------------------读取文件
                fs.read({
                    fd: ret.fd,
                    offset: 0,
                    length: 0
                }, function(ret, err) {
                    if (ret.status) {
                        // alert(JSON.stringify(ret));
                        var data = ret.data;
                        excSql(data);
                        // createTable(data);

                    } else {
                        alert(JSON.stringify(err));
                    }
                });
                //-------
            } else {
                alert(JSON.stringify(err));
            }
        });
    }

    function excSql(data) {
        var db = api.require('db');
        arr = data.split(';');
        alert(data);
        for (var i = 0; i < arr.length; i++) {
            db.executeSql({
                name: 'test',
                sql: arr[i]
            }, function(ret, err) {
                if (ret.status) {
                    // alert(JSON.stringify(ret));
                } else {
                    alert(JSON.stringify(err));
                }
            });
        }
    }


    function createTable(data) {
        var db = api.require('db');
        alert(data);
        db.executeSql({
            name: 'test',
            sql: data
        }, function(ret, err) {
            if (ret.status) {
                alert(JSON.stringify(ret));
            } else {
                alert(JSON.stringify(err));
            }
        });

    }


    function is_existence() {
        var fs = api.require('fs');
        fs.exist({
            path:'file:///storage/emulated/0/ader/sql/'
        }, function(ret, err) {
            if (ret.exist) {
                // alert(JSON.stringify(ret));
                copy();
            } else {
                // alert('is_existence没有新文件');
                openDatabase();
            }
        });

    }

    function openDatabase() {
        var db = api.require('db');
        db.openDatabase({
            name: 'data',
            path: 'fs://sql/data',
            // path: 'widget://res/data',
        }, function(ret, err) {
            if (ret.status) {
                // alert(JSON.stringify(ret));
            } else {
                alert(JSON.stringify(err));
            }
        });
    }

    function readDir() {
        var fs = api.require('fs');
        fs.readDir({
            path: 'fs://sql'
        }, function(ret, err) {
            if (ret.status) {
                alert(JSON.stringify(ret));
            } else {
                alert(JSON.stringify(err));
            }
        });
    }


    apiready = function() {
        $api.fixStatusBar($api.dom('header'));
is_existence();
        // copy();
        // readDir();


        api.openWin({
            name: 'login',
            url: './login.html',
        });



    }
</script>
